package com.siao.display
{
	import com.siao.component.TooltipsManager;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 提示元素
	 * @author SiaoLeon
	 */
	public class TooltipElement extends LayoutElement
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		private var _tooltip:Class;
		
		/**
		 * tooltip 提示类，需继承 Tooltip 类，并确保其已在 TooltipsManager 中注册。
		 * @return
		 */
		public function get tooltip():Class
		{
			return this._tooltip;
		}
		
		public function set tooltip(value:Class):void
		{
			if (this._tooltip != value)
			{
				TooltipsManager.instance.hide(this);
			}
			
			this._tooltip = value;
		}
		
		private var _tooltipData:Object;
		
		/**
		 * 提示数据
		 * @return
		 */
		public function get tooltipData():Object
		{
			return this._tooltipData;
		}
		
		public function set tooltipData(value:Object):void
		{
			this._tooltipData = value;
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function TooltipElement()
		{
			super();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		override protected function onAddedToStage():void
		{
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler, false, 0, true);
		}
		
		override protected function onRemovedFromStage():void
		{
			this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler);
			this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
			
			if (this.stage != null)
			{
				this.stage.removeEventListener(Event.MOUSE_LEAVE, onMouseOutHandler);
			}
		}
		
		/**
		 * 鼠标划过时
		 * @param event
		 */
		private function onMouseOverHandler(event:MouseEvent):void
		{
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler, false, 0, true);
			this.stage.addEventListener(Event.MOUSE_LEAVE, onMouseOutHandler, false, 0, true);
			
			TooltipsManager.instance.show(this);
		}
		
		/**
		 * 鼠标划出时
		 * @param event
		 */
		private function onMouseOutHandler(event:Event=null):void
		{
			this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
			
			if (this.stage != null)
			{
				this.stage.removeEventListener(Event.MOUSE_LEAVE, onMouseOutHandler);
			}
			
			TooltipsManager.instance.hide(this);
		}
	
	}
}
